import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provide/person.dart';
import './register.dart';

class PwdUpdPage extends StatefulWidget {
  final Widget child;

  PwdUpdPage({Key key, this.child}) : super(key: key);

  _PwdUpdPageState createState() => _PwdUpdPageState();
}

class _PwdUpdPageState extends State<PwdUpdPage> with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  Animation<double> _animation;

  TextEditingController _oriPwdController = new TextEditingController();
  TextEditingController _updPwdController = new TextEditingController();
  GlobalKey _formKey= new GlobalKey<FormState>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController = new AnimationController(
      vsync: this,duration: Duration(milliseconds: 2000)
    );

    _animation = new Tween(begin: 400.0,end: 0.0).animate(_animationController)
      ..addListener((){
        setState(() {
          
        });
      });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text("密码修改"),
        centerTitle: true,
      ),
      body: Center(
        child: Card(
          child: Container(
            padding: EdgeInsets.all(ScreenUtil().setWidth(50)),
            width: ScreenUtil().setWidth(550),
            height: ScreenUtil().setHeight(600),
            child: Form(
              key: _formKey, //设置globalKey，用于后面获取FormState
              autovalidate: true, //开启自动校验
              child: Column(
                children: <Widget>[
                  TextFormField(
                      autofocus: true,
                      controller: _oriPwdController,
                      decoration: InputDecoration(
                          labelText: "原密码",
                          hintText: "原密码",
                          icon: Icon(Icons.lock)
                      ),
                      obscureText: true,
                      // 校验用户名
                      validator: (v) {
                        return v
                            .trim()
                            .length > 0 ? null : "原密码不能为空";
                      }

                  ),
                  TextFormField(
                      controller: _updPwdController,
                      decoration: InputDecoration(
                          labelText: "新密码",
                          hintText: "新的登录密码",
                          icon: Icon(Icons.lock)
                      ),
                      obscureText: true,
                      //校验密码
                      validator: (v) {
                        return v
                            .trim()
                            .length > 5 ? null : "新密码不能少于6位";
                      }
                  ),
                  // 登录按钮
                  InkWell(
                    onTap: (){
                      login(context);
                    },
                    child: Container(
                      margin: EdgeInsets.only(top: 30),
                      height: ScreenUtil().setHeight(75),
                      width: _animation.value>42?_animation.value:42,
                      decoration:BoxDecoration(
                        borderRadius: BorderRadius.circular(30),
                        color: Theme.of(context).primaryColor,
                      ),
                      alignment: Alignment.center,
                      child: _animation.value > 75? new Text("确定",
                              style: TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.bold,
                                color: Colors.white
                                )
                              )
                              : CircularProgressIndicator(
                                valueColor: AlwaysStoppedAnimation(Colors.white),
                              )
                      ),
                  ),
                  SizedBox(
                    height: ScreenUtil().setHeight(5),
                  ),
                ]
              )
            ),
          )
        ),
      )
    );
  }

  // 修改密码
  void login(context) async {
    if((_formKey.currentState as FormState).validate()){
      await _animationController.forward();
      await Provide.value<PersonnalProvider>(context).pwdUpd(_oriPwdController.text, _updPwdController.text,context);
      await _animationController.reverse();
    }
    
  }
}